﻿using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Edu.Http;
using Edu.Http.Extensions;
using Edu.Http.Proxy;
using Infrastructure.Config.Core.External;
using Infrastructure.ExternalService.Core.Imp.Extensions;
using Infrastructure.ExternalService.Core.PayCenter;
using Infrastructure.ExternalService.Core.Requests;
using Infrastructure.ExternalService.Core.Responses;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace Infrastructure.ExternalService.Core.Imp.PayCenter
{
    public class PayCenterClient : BaseHttpClient, IPayCenterClient
    {
        private readonly OrderCenterApiOptions _orderingOptions;
        private readonly string formUrlencoded = "application/x-www-form-urlencoded";

        public IAccessTokenProvider TokenProvider { get; set; }

        public PayCenterClient(IOptions<OrderCenterApiOptions> options
            , HttpClient client
            , ILoggerFactory loggerFactory)
            : base(client, loggerFactory, options.Value.PayCenterOptions.EndPoint)
        {
            _orderingOptions = options.Value ?? throw new HttpProxyException($"{nameof(PayCenterClient)}配置获取失败");
            loggerFactory.CreateLogger<PayCenterClient>();
        }


        #region 接口实现方法

        /// <summary>
        /// 获取所有可用的支付账户信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<List<PayAccountResponse>> GetPayAccountsAsync(GetPayAccountsRequest request)
        {
            var formStr = ConvertRequestToCommonFormUrlEncode(request);
            var response = await PostJsonAsync<WrapResponse<List<PayAccountResponse>>>(url: _orderingOptions.PayCenterOptions.GetPayAccounts, jsonStr: formStr, formUrlencoded, headers: null);

            if (response != null && response.Success && response.Data != null)
            {
                return response.Data;
            }

            if (response.Data == null)
            {
                throw new HttpProxyException("支付账户信息不存在");
            }

            throw new HttpProxyException($"{response.Msg}");
        }

        /// <summary>
        /// 获取所有可用的支付账户信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<PayAccountResponse> GetPayAccountAsync(GetPayAccountsRequest request)
        {
            var formStr = ConvertRequestToCommonFormUrlEncode(request);
            var response = await PostJsonAsync<WrapResponse<List<PayAccountResponse>>>(url: _orderingOptions.PayCenterOptions.GetPayAccounts, jsonStr: formStr, formUrlencoded, headers: null);

            if (response != null && response.Success && response.Data != null)
            {
                var list = response.Data;
                return list?.FirstOrDefault();
            }

            if (response.Data == null)
            {
                throw new HttpProxyException("支付账户信息不存在");
            }

            throw new HttpProxyException($"{response.Msg}");
        }

        /*
        public async Task<PayPreCreateResponse> PayPreCreateAsync(PayPreCreateRequest request)
        {
            var formStr = ConvertRequestToCommonFormUrlEncode(request);
            var response = await PostJsonAsync<Responses.WrapResponse<PayPreCreateResponse>>(url: _orderingOptions.PayCenterOptions.PreCreate, jsonStr: formStr, formUrlencoded, headers: null);
            if (response.Code.Equals(ReturnCode.Success) && response.Data != null)
            {
                return response.Data;
            }
            throw new HttpProxyException($"{response.Msg}");
        }

        public async Task<PayResponse> PayAsync(PayRequest request)
        {
            var formStr = ConvertRequestToCommonFormUrlEncode(request);
            var response = await PostJsonAsync<Responses.WrapResponse<PayResponse>>(url: _orderingOptions.PayCenterOptions.Pay, jsonStr: formStr, formUrlencoded, headers: null);
            if (response.Code.Equals(ReturnCode.Success) && response.Data != null)
            {
                return response.Data;
            }
            throw new HttpProxyException($"{response.Msg}");
        }*/
        public async Task<PayPreCreateResponse> PayPreCreateAsync(PayPreCreateRequest request)
        {
            var formStr = ConvertRequestToCommonFormUrlEncode(request);
            var response = await PostJsonAsync<WrapResponse<PayPreCreateResponse>>(url: _orderingOptions.PayCenterOptions.PreCreate, jsonStr: formStr, formUrlencoded, headers: null);

            if (response != null && response.Success && response.Data != null)
            {
                return response.Data;
            }
            throw new HttpProxyException($"{response.Msg ?? "网络繁忙，请稍后重试！"}");
        }

        //public async Task<PayResponse> PayAsync(PayRequest request)
        //{
        //    var formStr = ConvertRequestToCommonFormUrlEncode(request);
        //    var response = await PostJsonAsync<WrapResponse<PayResponse>>(url: _orderingOptions.PayCenterOptions.Pay, jsonStr: formStr, formUrlencoded, headers: null);

        //    if (response != null && response.Success && response.Data != null)
        //    {
        //        return response.Data;
        //    }
        //    throw new HttpProxyException($"{response.Msg ?? "网络繁忙，请稍后重试！"}");
        //}

        //public async Task<PayQueryResponse> PayQueryAsync(PayQueryRequest request)
        //{
        //    var formStr = ConvertRequestToCommonFormUrlEncode(request);
        //    var response = await PostJsonAsync<WrapResponse<PayQueryResponse>>(url: _orderingOptions.PayCenterOptions.Query, jsonStr: formStr, formUrlencoded, headers: null);

        //    if (response != null && response.Success && response.Data != null)
        //    {
        //        return response.Data;
        //    }

        //    throw new HttpProxyException($"{response.Msg}");
        //}

        #endregion
    }
}